package com.blue.eagle.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blue.eagle.dao.CustomerDao;
import com.blue.eagle.domain.Customer;

@Service
public class ExcelParser {
	
	/**
	 * @uml.property  name="customerDao"
	 * @uml.associationEnd  readOnly="true"
	 */
	@Autowired
	CustomerDao customerDao ;
	
	public void parseFile(File inFile) throws FileNotFoundException, IOException {
		
		System.out.println("File Absoulte Name : "+inFile.getAbsolutePath());
		
		List<Customer> customerList = new ArrayList<Customer>();
		
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inFile.getAbsolutePath()));
		
		HSSFSheet sheet = workbook.getSheetAt(0);
		Customer customer = null;
		for(int i = 1 ;i <= 5; i++) {
			
			HSSFRow row = sheet.getRow(i);
			customer = new Customer ();
			customer.setFirstName(row.getCell(0).getRichStringCellValue().getString());
			customer.setLastName(row.getCell(1).getRichStringCellValue().getString());
			customer.setMobileNumber(String.valueOf(row.getCell(2).getNumericCellValue()));
			customer.setUserName(row.getCell(3).getRichStringCellValue().getString());
			customer.setPassword(String.valueOf(row.getCell(4).getNumericCellValue()));
			
			customerList.add(customer);
			customerDao.save(customer);
			
			System.out.println("Customer : "+customer.getFirstName());
		}
		
		
	}

}
